package com.dc.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {



        System.out.println("进入session过滤器");


        HttpServletResponse response=(HttpServletResponse)resp;
        HttpServletRequest request=(HttpServletRequest)req;
        HttpSession session=((HttpServletRequest) req).getSession();
        //获取
        if(session.getAttribute("username")!=null){
            chain.doFilter(req,resp);
        }else {
         resp.getWriter().write("账号密码错误，请重新登录");
         resp.getWriter().write("");
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
